package aiP.A3.Client.Monitor;

import aiP.A3.AuftragKomponente.DataAccessLayer.Entity.Angebot;
import aiP.A3.AuftragKomponente.DataAccessLayer.Entity.Auftrag;
import aiP.A3.MPS.Interfaces.IServerInstance;
import aiP.A3.MPS.ServerInstance;
import aiP.A3.PersistenceService.Persist;
import aiP.A3.PersistenceService.iEntity;

import java.rmi.ConnectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Andreas
 * Date: 26.05.14
 * Time: 10:47
 * To change this template use File | Settings | File Templates.
 */
public class Instance {

    private final String id;
    private Date zuletztOnline = new Date();

    // private Date lebendigSeit;
    private Date lebendigSeit = new Date();                      // Up-time
    // private Date totSeit;
    private Date totSeit = null;                           // Down-time


    private IServerInstance instance = null;

    public IServerInstance getInstance() {
        return instance;
    }

    public Instance(String id) {
        this.id = id;
        //this.lebendigSeit = new Date();

        try {
            instance = getStub();
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NotBoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public String getId() {
        return id;
    }

    public boolean isLebendig() {
       // return lebendigSeit != null && totSeit == null;
        try {
            return getInstance().isAlive();
        } catch (ConnectException ce){
            // ToDo WEnn diese Fliegt ist die Instance raus... muss also auch aus der Map gelöscht werden
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NotBoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return false;
    }

    public boolean isTot() {
        try {
            return !getInstance().isAlive();
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NotBoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return false;
    }

    void setLebendig() {
        try {
            instance.setAlive(true);
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NotBoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    void setTot() {
        this.totSeit = new Date();
        try {
            instance.setAlive(false);
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NotBoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    void updateZuletztOnline() {
        this.zuletztOnline = new Date();
    }

    Date getLebendigSeit() {
        return lebendigSeit;
    }

    public long getSekundenOnline() {
        if (this.lebendigSeit == null) {
            return -1;
        } else {
            return (new Date().getTime() - this.lebendigSeit.getTime()) / 1000;
        }
    }

    public long getSekundenOffline() {
        if (this.totSeit == null) {
            return -1;
        } else {
            return (new Date().getTime() - this.totSeit.getTime()) / 1000;
        }
    }

    public long getZuletztOnlineInMilli() {
        return new Date().getTime() - this.zuletztOnline.getTime();
    }


    public void start() throws RemoteException, NotBoundException {
        getStub().startEKG();
        System.out.println("start_getStub"+ getStub().toString());
    }

    public void stop() throws RemoteException, NotBoundException {
        System.out.println("stop_getStub"+ getStub().toString());
        getStub().stopEKG();
    }


    public IServerInstance getStub() throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(1173);
        if(instance == null){
            instance = (IServerInstance) registry.lookup(this.getId().toUpperCase());
        }

        return instance;
    }

    public String toString() {
        String result = "";
        if (isLebendig())
            result = "Instance: " + getId() + " lebt: " + isLebendig() + " seit: " + getSekundenOnline();
        else
            result = "Instance: " + getId() + " tot: " + isTot() + " seit: " + getSekundenOffline();
        return result;
    }

}
